package com.antdesignbackend.backend.information.mechanism.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.antdesignbackend.backend.entity.Enterprise;
import com.antdesignbackend.backend.entity.Users;
import com.antdesignbackend.backend.service.EnterpriseService;
import com.antdesignbackend.backend.service.UserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.information.mechanism.entity.Mechanism;
import com.antdesignbackend.backend.information.mechanism.entity.entityVo.MechanismVo;
import com.antdesignbackend.backend.information.mechanism.service.MechanismService;

/**
 * 录入机构
 *
 * @author 不二
 * @email jack.com
 * @date 2020-09-18 14:04:59
 */

@Controller
@RequestMapping("/mechanism")
@Slf4j
public class MechanismController extends BaseController{

	private static final String FUN_NAME = "录入机构";//功能名称

	@Autowired
	private MechanismService mechanismService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
	private UserService userService;

	/**
	 * 动物诊疗机构
	 * @return
	 */
	@GetMapping("/medical")
	@AuthToken
	public String medical() {
		return "information/mechanism/medical";
	}

	/**
	 * 饲料和饲料添加剂生产企业
	 * @return
	 */
	@GetMapping("/additiveproduction")
	@AuthToken
	public String additiveproduction() {
		return "information/mechanism/additiveProduction";
	}

	/**
	 * 饲料和饲料添加剂经营企业
	 * @return
	 */
	@GetMapping("/additivemanagement")
	@AuthToken
	public String additivemanagement() {
		return "information/mechanism/additivemanagement";
	}

	/**
	 * 兽药经营企业
	 * @return
	 */
	@GetMapping("/veterinarymedicine")
	@AuthToken
	public String veterinarymedicine() {
		return "information/mechanism/veterinarymedicine";
	}


	@PostMapping("/tableSearch")
	@ResponseBody
	ReturnVO tableSearch(@RequestBody MechanismVo mechanismVo) throws SecurityException, IllegalArgumentException{
		ReturnVO result = new ReturnVO();
		List<MechanismVo> list = new ArrayList<>();
		QueryWrapper<MechanismVo> wrapper = new QueryWrapper<>();
		wrapper.eq("m.types",mechanismVo.getTypes());
		if (mechanismVo.getName() != null && !mechanismVo.getName().equals("")){
			wrapper.like("m.name",mechanismVo.getName());
		}
		if (mechanismVo.getUserName() != null && !mechanismVo.getUserName().equals("")){
			wrapper.like("m.userName",mechanismVo.getUserName());
		}
		if (mechanismVo.getPhone() != null && !mechanismVo.getPhone().equals("")){
			wrapper.like("m.phone",mechanismVo.getPhone());
		}
		int total = mechanismService.count(wrapper);
		if (total > 0){
			list = mechanismService.search(wrapper,new Page<>(mechanismVo.getPageIndex(),mechanismVo.getPageSize()));
		}
		result.setTotal(total);
		result.setData(list);
		return result;

	}

	@GetMapping("/getById/{id}")
	@ResponseBody
	public	Mechanism getById(@PathVariable(name = "id") String id) {
		Mechanism obj = mechanismService.getById(id);
		return obj;
	}

	@PostMapping("/save")
	@ResponseBody
	public ReturnVO save(@RequestBody Mechanism mechanism){
		ReturnVO result = new ReturnVO();
		mechanism.setCreateUid(getLoginUserId());
		mechanism.setCreateTime(new Date());
		if(mechanismService.save(mechanism)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	@PostMapping("update")
	@ResponseBody
	public ReturnVO updateById(@RequestBody Mechanism mechanism){
		ReturnVO result = new ReturnVO();
		if(mechanismService.updateById(mechanism)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	/**
     * 根据id删除
     */
	@DeleteMapping("/deleteById/{id}")
	@ResponseBody
	public ReturnVO deleteById(@PathVariable Serializable id) {
		ReturnVO result = new ReturnVO();
		if (mechanismService.removeById(Integer.parseInt(id.toString())) == true) {
			result.setMessage("删除成功");
		} else {
			result.setMessage("删除失败");
			result.setCode("0");
		}
		return result;
	}
	@PostMapping("/getName")
	@ResponseBody
	ReturnVO getName(@RequestBody Mechanism mechanism) throws SecurityException, IllegalArgumentException{
		ReturnVO result = new ReturnVO();
		QueryWrapper<Mechanism> wrapper = new QueryWrapper<>();
		if(mechanism.getTypes()!=null && !mechanism.getTypes().equals("")){
			wrapper.eq("types",mechanism.getTypes());
		}
		if(mechanism.getId()!=null && !mechanism.getId().equals("")){
			wrapper.eq("id",mechanism.getId());
		}
		//判断用户权限
		Users users = userService.getById(getLoginUserId());
		if (users.getAuthType() == 1){ //地市权限
			wrapper.eq("cityCode",users.getAuthCity());
		}else if (users.getAuthType() == 2){ //区县权限
			wrapper.eq("districtCode",users.getAuthDistrict());
		}else if (users.getAuthType() == 3){
			wrapper.eq("townCode",users.getAuthTown());
		}else {
			return result;
		}
		List<Mechanism> list = mechanismService.list(wrapper);
		result.setData(list);
		return result;
	}
    @PostMapping("/getEnterprise")
    @ResponseBody
    ReturnVO getEnterprise(@RequestBody Enterprise enterprise) throws SecurityException, IllegalArgumentException{
        ReturnVO result = new ReturnVO();
        QueryWrapper<Enterprise> wrapper = new QueryWrapper<>();
        if(enterprise.getTypes()!=null && !enterprise.getTypes().equals("")){
            wrapper.eq("types",enterprise.getTypes());
        }
        if(enterprise.getEnterpriseId()!=null && !enterprise.getEnterpriseId().equals("")){
            wrapper.eq("enterpriseId",enterprise.getEnterpriseId());
        }
		//判断用户权限
		Users users = userService.getById(getLoginUserId());
		if (users.getAuthType() == 1){ //地市权限
			wrapper.eq("cityCode",users.getAuthCity());
		}else if (users.getAuthType() == 2){ //区县权限
			wrapper.eq("districtCode",users.getAuthDistrict());
		}else if (users.getAuthType() == 3){
			wrapper.eq("townCode",users.getAuthTown());
		}else {
			return result;
		}
		wrapper.eq("examine",1).eq("activity",1);
        List<Enterprise> list = enterpriseService.list(wrapper);
        result.setData(list);
        return result;
    }
}
